package xlingpaper.xxe;

/*
 * JFontChooser.java
 *
 * Created on December 2, 2002, 9:54 AM
 */

/**
 *
 * @author  TDuffy
 *
 */

/* Modified December 28, 2009 by Andy Black */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;

import com.xmlmind.guiutil.Alert;
import xlingpaper.xxe.*;

public class XLingPaperFontInfoChooserDialog extends javax.swing.JDialog {
    /**
     * 
     */
    final String ksUseDefault = "Use default";
    final String ksNormal = "Normal";
    final String ksBold = "Bold";
    final String ksItalic = "Italic";
    final String ksBoldItalic = "Bold Italic";
    final String ksDefaultFontFamily = "Times New Roman";
    private static final long serialVersionUID = 1L;
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    // the font
    private XLingPaperFontInfo fontInfo;
    final int numButtons = 2;
    JRadioButton[] radioButtons = new JRadioButton[numButtons];

    // Constructors
    /** Creates new form JFontChooser */
    public XLingPaperFontInfoChooserDialog(java.awt.Frame parent, XLingPaperFontInfo fontInfo) {
	super(parent);
	this.fontInfo = fontInfo;
	initComponents();
	lblPreview.setFont(fontInfo);
    }

    public XLingPaperFontInfoChooserDialog(java.awt.Frame parent) {
	super(parent);
	this.fontInfo = new XLingPaperFontInfo("Dialog", Font.PLAIN, 12);
	initComponents();
	lblPreview.setFont(fontInfo);
    }

    public XLingPaperFontInfoChooserDialog(XLingPaperFontInfo fontInfo) {
	super((javax.swing.JFrame) null);
	this.fontInfo = fontInfo;
	initComponents();
	lblPreview.setFont(fontInfo);
    }

    public XLingPaperFontInfoChooserDialog() {
	super((javax.swing.JFrame) null);
	this.fontInfo = new XLingPaperFontInfo("Dialog", Font.PLAIN, 12);
	initComponents();
	lblPreview.setFont(fontInfo);
    }

    /** @return the font chosen by the user */
    public XLingPaperFontInfo getFontInfo() {
	return fontInfo;
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
	return returnStatus;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    private void initComponents() {// GEN-BEGIN:initComponents
	java.awt.GridBagConstraints gridBagConstraints;

	mainPanel = new javax.swing.JPanel();
	fontPanel = new javax.swing.JPanel();
	jLabel1 = new javax.swing.JLabel();
	jLabel2 = new javax.swing.JLabel();
	jLabel3 = new javax.swing.JLabel();
	jScrollPane1 = new javax.swing.JScrollPane();
	String[] systemFontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment()
		.getAvailableFontFamilyNames();
	String[] fontFamilyNames = new String[1 + systemFontFamilyNames.length];
	fontFamilyNames[0] = ksUseDefault;
	for (int i = 0; i < systemFontFamilyNames.length; i++) {
	    fontFamilyNames[i + 1] = systemFontFamilyNames[i];
	}
	lstFont = new javax.swing.JList(fontFamilyNames);
	jScrollPane2 = new javax.swing.JScrollPane();
	lstStyle = new javax.swing.JList();
	jScrollPane3 = new javax.swing.JPanel();
	lstSize = new javax.swing.JComboBox();
	previewPanel = new javax.swing.JPanel();
	lblPreview = new javax.swing.JLabel();
	buttonPanel = new javax.swing.JPanel();
	okButton = new javax.swing.JButton();
	cancelButton = new javax.swing.JButton();

	setTitle("Choose Font Information");
	setModal(true);
	setResizable(false);
	addWindowListener(new java.awt.event.WindowAdapter() {
	    public void windowClosing(java.awt.event.WindowEvent evt) {
		closeDialog(evt);
	    }
	});

	mainPanel.setLayout(new java.awt.GridLayout(2, 1));

	fontPanel.setLayout(new java.awt.GridBagLayout());

	jLabel1.setText("Font Family");
	gridBagConstraints = new java.awt.GridBagConstraints();
	gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
	gridBagConstraints.weightx = 2.0;
	fontPanel.add(jLabel1, gridBagConstraints);

	jLabel2.setText("Font Style");
	gridBagConstraints = new java.awt.GridBagConstraints();
	gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
	fontPanel.add(jLabel2, gridBagConstraints);

	jLabel3.setText("Font Size (% or pt)");
	gridBagConstraints = new java.awt.GridBagConstraints();
	gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
	gridBagConstraints.weightx = 0.2;
	fontPanel.add(jLabel3, gridBagConstraints);

	lstFont.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
	lstFont.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
	    public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
		lstFontValueChanged(evt);
	    }
	});
	jScrollPane1.setViewportView(lstFont);
	lstFont.setSelectedValue(fontInfo.getFontFamily(), true);

	gridBagConstraints = new java.awt.GridBagConstraints();
	gridBagConstraints.gridx = 0;
	gridBagConstraints.gridy = 1;
	gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	gridBagConstraints.ipadx = 1;
	gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
	gridBagConstraints.weightx = 2.0;
	fontPanel.add(jScrollPane1, gridBagConstraints);

	initStyleList();

	jScrollPane2.setViewportView(lstStyle);

	gridBagConstraints = new java.awt.GridBagConstraints();
	gridBagConstraints.gridx = 1;
	gridBagConstraints.gridy = 1;
	gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	gridBagConstraints.ipadx = 1;
	gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
	fontPanel.add(jScrollPane2, gridBagConstraints);

	initPercentagePointRadioGroup();

	jScrollPane3.setLayout(new GridBagLayout());
	GridBagConstraints gbc = new GridBagConstraints();
	gbc.gridx = 1;
	gbc.gridy = GridBagConstraints.RELATIVE;
	gbc.fill = GridBagConstraints.HORIZONTAL;
	for (int i = 0; i < numButtons; i++) {
	    jScrollPane3.add(radioButtons[i], gbc);
	}

	String[] strings = { "Initial Value", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
		"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
		"28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41",
		"42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55",
		"56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
		"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83",
		"84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97",
		"98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109",
		"110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121",
		"122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133",
		"134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145",
		"146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157",
		"158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169",
		"170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181",
		"182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193",
		"194", "195", "196", "197", "198", "199", "200", ksUseDefault };
	strings[0] = this.fontInfo.getFontSize();
	lstSize = new JComboBox(strings);
	lstSize.setEditable(true);
	lstSize.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		lstSizeValueChanged(evt);
	    }
	});
	gbc.gridy = GridBagConstraints.RELATIVE;
	jScrollPane3.add(lstSize, gbc);

	gridBagConstraints = new java.awt.GridBagConstraints();
	gridBagConstraints.gridx = 2;
	gridBagConstraints.gridy = 1;
	gridBagConstraints.fill = java.awt.GridBagConstraints.RELATIVE;
	gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
	gridBagConstraints.ipadx = 1;
	gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
	gridBagConstraints.weightx = 0.2;
	// fontPanel.add(jScrollPane3, gridBagConstraints);
	fontPanel.add(jScrollPane3, gridBagConstraints);

	mainPanel.add(fontPanel);

	previewPanel.setLayout(new java.awt.BorderLayout());

	previewPanel.setBorder(new javax.swing.border.TitledBorder(null, "Preview",
		javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
		javax.swing.border.TitledBorder.DEFAULT_POSITION,
		new java.awt.Font("Dialog", 0, 12)));
	lblPreview.setFont(new java.awt.Font("Dialog", 0, 12));
	lblPreview.setText("ABCDEFG abcdefg");
	previewPanel.add(lblPreview, java.awt.BorderLayout.CENTER);

	mainPanel.add(previewPanel);

	getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

	buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

	okButton.setText("OK");
	okButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		okButtonActionPerformed(evt);
	    }
	});

	buttonPanel.add(okButton);

	cancelButton.setText("Cancel");
	cancelButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		cancelButtonActionPerformed(evt);
	    }
	});

	buttonPanel.add(cancelButton);

	getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);

	pack();
	java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	setSize(new java.awt.Dimension(443, 429));
	setLocation((screenSize.width - 443) / 2, (screenSize.height - 429) / 2);
    }// GEN-END:initComponents

    private void initPercentagePointRadioGroup() {
	radioButtons[0] = new JRadioButton("%");
	radioButtons[0].setActionCommand(percentageCommand);

	radioButtons[1] = new JRadioButton("pt");
	radioButtons[1].setActionCommand(pointsCommand);

	for (int i = 0; i < numButtons; i++) {
	    group.add(radioButtons[i]);
	}

	if (fontInfo.getUsePercentage()) {
	    radioButtons[0].setSelected(true);
	} else {
	    radioButtons[1].setSelected(true);
	}
	radioButtons[0].addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		radioButtonActionPerformed(evt);
	    }
	});
	radioButtons[1].addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		radioButtonActionPerformed(evt);
	    }
	});
    }

    public void radioButtonActionPerformed(ActionEvent e) {
	sizeType type = sizeType.percentage;

	if (pointsCommand.equals(group.getSelection().getActionCommand())) {
	    type = sizeType.point;
	    fontInfo.setUsePoints(true);
	} else if (percentageCommand.equals(group.getSelection().getActionCommand())) {
	    type = sizeType.percentage;
	    fontInfo.setUsePercentage(true);
	}
	double size;
	String sValue = fontInfo.getFontSize();
	try {
	    size = Float.parseFloat(sValue);
	} catch (NumberFormatException nfe) {
	    size = 12;
	}
	if (type == sizeType.percentage) {
	    size = size * .01 * 12;
	}
	fontInfo.setFontSizeToShow((int)size);
	showFontInfo();
    }

    private void initStyleList() {
	lstStyle.setModel(new javax.swing.AbstractListModel() {
	    String[] strings = { ksUseDefault, ksNormal, ksBold, ksItalic, ksBoldItalic };

	    public int getSize() {
		return strings.length;
	    }

	    public Object getElementAt(int i) {
		return strings[i];
	    }
	});
	lstStyle.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
	lstStyle.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
	    public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
		lstStyleValueChanged(evt);
	    }
	});
	int iStyleIndex = 0;
	if (fontInfo.getUseStyleDefault() && fontInfo.getUseWeightDefault()) {
	    iStyleIndex = 0;
	} else {
	    if (fontInfo.isPlain()) {
		iStyleIndex = 1;
	    } else {
		if (fontInfo.isBold() && !fontInfo.isItalic()) {
		    iStyleIndex = 2;
		} else {
		    if (!fontInfo.isBold() && fontInfo.isItalic()) {

			iStyleIndex = 3;
		    } else {
			if (fontInfo.isItalic() && fontInfo.isBold()) {
			    iStyleIndex = 4;
			}
		    }
		}
	    }
	}
	lstStyle.setSelectedIndex(iStyleIndex);
    }

    private void lstStyleValueChanged(javax.swing.event.ListSelectionEvent evt) {// GEN
	// -
	// FIRST
	// :
	// event_lstStyleValueChanged
	int style = -1;
	String selStyle = (String) lstStyle.getSelectedValue();
	if (selStyle == ksNormal) {
	    style = Font.PLAIN;
	    fontInfo.setStylePlain(true);
	    fontInfo.setStyleBold(false);
	    fontInfo.setStyleItalic(false);
	}
	if (selStyle == ksBold) {
	    style = Font.BOLD;
	    fontInfo.setStyleBold(true);
	    fontInfo.setStyleItalic(false);
	    fontInfo.setStylePlain(false);
	}
	if (selStyle == ksItalic) {
	    style = Font.ITALIC;
	    fontInfo.setStyleItalic(true);
	    fontInfo.setStyleBold(false);
	    fontInfo.setStylePlain(false);
	}
	if (selStyle == ksBoldItalic) {
	    style = Font.BOLD + Font.ITALIC;
	    fontInfo.setStyleBold(true);
	    fontInfo.setStyleItalic(true);
	    fontInfo.setStylePlain(false);
	}
	if (selStyle == ksUseDefault) {
	    style = Font.PLAIN;
	    fontInfo.setUseStyleDefault(true);
	    fontInfo.setUseWeightDefault(true);
	    fontInfo.setStylePlain(false);
	    fontInfo.setStyleBold(false);
	    fontInfo.setStyleItalic(false);
	}
	fontInfo.setFontStyleToShow(style);
	showFontInfo();
    }// GEN-LAST:event_lstStyleValueChanged

    public void lstSizeValueChanged(ActionEvent evt) {
	String sResult = ((String) lstSize.getSelectedItem()).trim();
	String sValue = sResult;
	if (sValue.endsWith("pt")) {
	    sValue = sValue.substring(0, sValue.length() - 2);
	    radioButtons[1].setSelected(true);
	    fontInfo.setUsePoints(true);
	} else {
	    if (sValue.endsWith("%")) {
		sValue = sValue.substring(0, sValue.length() - 1);
		fontInfo.setUsePercentage(true);
		radioButtons[0].setSelected(true);
	    }
	}
	sizeType type = sizeType.percentage;
	fontInfo.setUseSizeDefault(false);
	if (ksUseDefault.equals(sResult)) {
	    type = sizeType.ignore;
	    sValue = "12";
	    fontInfo.setUseSizeDefault(true);
	} else {
	    if (pointsCommand.equals(group.getSelection().getActionCommand())) {
		type = sizeType.point;
		fontInfo.setUsePoints(true);
	    } else if (percentageCommand.equals(group.getSelection().getActionCommand())) {
		type = sizeType.percentage;
		fontInfo.setUsePercentage(true);
	    }
	}
	double size;
	Boolean fNumberOK = true;
	try {
	    size = Float.parseFloat(sValue);
	} catch (NumberFormatException nfe) {
	    Alert
		    .showError(
			    this.mainPanel,
			    "Sorry, but the contents need to be the words 'Use default' or a (possibly decimal) number optionally followed by 'pt' or '%'.");
	    size = 12;
	    fNumberOK = false;
	}
	if (fNumberOK) {
	    if (type == sizeType.percentage) {
		size = size * .01 * 12;
	    }
	    fontInfo.setFontSize(sValue);
	    fontInfo.setFontSizeToShow((int) size);
	    showFontInfo();
	}
    }

    /*
     * private void lstSizeValueChanged(javax.swing.event.ListSelectionEvent
     * evt) {//GEN-FIRST:event_lstSizeValueChanged int size =
     * Integer.parseInt((String)lstSize.getSelectedValue()); font = new
     * Font(font.getFamily(),font.getStyle(),size); lblPreview.setFont(font);
     * }//GEN-LAST:event_lstSizeValueChanged
     */
    private void lstFontValueChanged(javax.swing.event.ListSelectionEvent evt) {// GEN
	// -
	// FIRST
	// :
	// event_lstFontValueChanged
	this.fontInfo.setFontFamily((String) lstFont.getSelectedValue());
	showFontInfo();
    }// GEN-LAST:event_lstFontValueChanged

    private void showFontInfo() {
	String sNewFamily = this.fontInfo.getFontFamily();
	String sNewFamilyToShow = sNewFamily;
	if (ksUseDefault.equals(sNewFamily)) {
	    sNewFamilyToShow = ksDefaultFontFamily;
	    this.fontInfo.setUseFamilyDefault(true);
	} else {
	    this.fontInfo.setUseFamilyDefault(false);
	}
	
	XLingPaperFontInfo tempFontInfo = new XLingPaperFontInfo(sNewFamilyToShow, fontInfo.getFontStyleToShow(), fontInfo.getFontSizeToShow());
	lblPreview.setFont(tempFontInfo);
    }

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
	// FIRST
	// :
	// event_okButtonActionPerformed
	doClose(RET_OK);
    }// GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN
	// -
	// FIRST
	// :
	// event_cancelButtonActionPerformed
	doClose(RET_CANCEL);
    }// GEN-LAST:event_cancelButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {// GEN-FIRST:
	// event_closeDialog
	doClose(RET_CANCEL);
    }// GEN-LAST:event_closeDialog

    private void doClose(int retStatus) {
	returnStatus = retStatus;
	setVisible(false);
    }

    enum sizeType {
	ignore, point, percentage
    };

    final ButtonGroup group = new ButtonGroup();

    final String pointsCommand = "pt";
    final String percentageCommand = "percentage";

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel fontPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JComboBox lstSize;
    private javax.swing.JButton okButton;
    private javax.swing.JList lstFont;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstStyle;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel previewPanel;
    private javax.swing.JLabel lblPreview;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel jScrollPane3;
    // End of variables declaration//GEN-END:variables

    private int returnStatus = RET_CANCEL;
}
